package com.wws.family.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import com.wika.basics.base.BaseActivity
import com.wika.basics.ext.divider
import com.wika.basics.ext.toColorSpan
import com.wika.basics.ext.vertical
import com.wws.family.R
import com.wws.family.adapter.MedicineAdapter
import com.wws.family.databinding.ActivityMedicineBinding
import com.wws.family.db.MedicineBean
import com.wws.family.db.WiKaDataBase
import com.wws.family.dialog.CommonDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@Description: 药品页面
 *@Date: 2025/1/7 11:41
 *@Author: WangWeiShuo
 */
class MedicineActivity: BaseActivity<ActivityMedicineBinding>() {
    private lateinit var launcher: ActivityResultLauncher<Intent>
    private var medicineList = mutableListOf<MedicineBean>()
    private var medicineAdapter: MedicineAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(R.drawable.baseline_arrow_back,"药箱",R.color.black,R.color.blue_B8DCFF,R.drawable.icon_add,true,true)
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            val intent = it.data
            if(intent != null){
                val medicineBean =  intent.getSerializableExtra("medicineBean") as MedicineBean
                if(medicineBean != null){
                    medicineList.add(0,medicineBean)
                    if(medicineAdapter != null){
                        medicineAdapter?.setItem(0,medicineBean)
                    }
                    checkData()
                }
            }
        }
    }

    override fun initData() {
        initMedicine()
    }

    override fun needFullScreen(): Boolean {
        return false
    }

    override fun actionBarRightClick() {
        val intent = Intent(this@MedicineActivity,MedicineAddActivity::class.java)
        intent.putExtra("isEdit",false)
        launcher.launch(intent)
    }

    private fun initMedicine(){
        lifecycleScope.launch(Dispatchers.IO){
            medicineList = WiKaDataBase.getInstance().medicineDao().getMedicineInfo()
            withContext(Dispatchers.Main){
                showMedicineInfo()
            }
        }
    }

    private fun checkData(){
        if(medicineList.isNullOrEmpty()){
            mBinding.ivNoData.visibility = View.VISIBLE
            mBinding.rvMedicine.visibility = View.INVISIBLE
        }else{
            mBinding.ivNoData.visibility = View.INVISIBLE
            mBinding.rvMedicine.visibility = View.VISIBLE
        }
    }

    private fun showMedicineInfo(){
        checkData()
        if(medicineAdapter == null){
            medicineAdapter = MedicineAdapter(this)
            mBinding.rvMedicine.vertical()
//            mBinding.rvMedicine.divider(color = getColor(R.color.gray_f0f0f0), paddingSize = 60, includeLast = false)
            mBinding.rvMedicine.adapter = medicineAdapter
        }
        medicineAdapter?.setData(medicineList)
        medicineAdapter?.addDeleteCallBack(object : MedicineAdapter.OnDeleteCallBack{
            override fun onDelete(pos: Int, itemBean: MedicineBean?) {
                showDeleteDialog(pos,itemBean)
            }
        })
    }

    private fun showDeleteDialog(pos: Int, itemBean: MedicineBean?){
        val deleteValue = "是否删除${itemBean?.name}?"
        val deleteContent = deleteValue.toColorSpan(IntRange(4,deleteValue.length - 1),getColor(R.color.red_DD2C00))
        CommonDialog.Builder(this).showTitle("删除提示").showContent(deleteContent)
            .setOnSureListener {
                medicineList.removeAt(pos)
                if(medicineAdapter != null){
                    medicineAdapter?.removeAt(pos)
                }
                checkData()
                lifecycleScope.launch(Dispatchers.IO){
                    WiKaDataBase.getInstance().medicineDao().deleteByName(itemBean?.name!!)
                }
            }.show()
    }
}